﻿using System.Collections;
using UnityEngine;

public class BattleSceneController : MonoBehaviour
{
    ViewControllerNav m_DisplayControllerNav;
    public ViewControllerNav battleSceneNav
    {
        get
        {
            if (m_DisplayControllerNav == null)
            {
                m_DisplayControllerNav = new GameObject("BattleSceneNav").AddComponent<ViewControllerNav>();
                this.m_DisplayControllerNav.transform.SetParent(transform);

                var UIChangeAnimPref = ResourceManager.Load<MainSceneUIChangeAnim>(ResourceManager.mainSceneUIChangeAnimPath);
                if(UIChangeAnimPref)
                    this.m_DisplayControllerNav.UIChangeAnimation = Instantiate<MainSceneUIChangeAnim>(UIChangeAnimPref);
            }
            return m_DisplayControllerNav;
        }
    }
    protected BattleController m_Controller;
    public BattleController battleController=> m_Controller;
    public void PlayBattleMusic()
    {
        PlayMusic(ResourceManager.battleSceneMusic);
    }

    public static void PlayMusic(string path)
    {
        AudioController.instance.StopAllMusic();
        var battleMusic = ResourceManager.Load<AudioClip>(path);

        if (battleMusic != null)
        {
            AudioController.instance.Play(battleMusic, Vector3.zero, true, null, true, .5f);
        }
    }

    public void GotoResultScene(BattleResult result)
    {
        PersonalResultPanelData resultPanelData = new PersonalResultPanelData();
        resultPanelData.level = result.level;
        resultPanelData.mapId = result.mapId;
        resultPanelData.win = result.win;
        resultPanelData.coin = (int) result.score;
        resultPanelData.star = result.star;

        var resultUI = this.battleSceneNav.LoadDisplay<ResultUIViewController>();

        if (Game.Instance)
            Game.Instance.user.UpdateCoin(Game.Instance.user.coin + result.score);

        //Game.Instance.user.UpdateLevelStar(result.level,result.star);

        resultUI.Init(resultPanelData);

        this.m_Controller.battleListener = null;
    }

    // Update is called once per frame
    void Update()
    {

    }
}